package com.nothing.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nothing.dao.UserFaceInfoDao;
import com.nothing.domain.UserFaceInfo;
import com.nothing.dto.FaceUserInfo;
import com.nothing.dto.ImageInfo;
import com.nothing.dto.ProcessInfo;
import com.nothing.hessian.config.HessionServiceConfig;
import com.nothing.hessian.service.CheckFaceService;
import com.nothing.service.FaceEngineService;
import com.nothing.service.UserFaceInfoService;
import com.nothing.util.ImageUtil;
import com.nothing.util.WorkId;
import com.xiaoleilu.hutool.date.DateUtil;

import cn.hutool.core.collection.CollectionUtil;


@Service
public class UserFaceInfoServiceImpl extends ServiceImpl<UserFaceInfoDao, UserFaceInfo> implements UserFaceInfoService {
    
    @Autowired
    private FaceEngineService        faceEngineService; 
   
	@Autowired
    private UserFaceInfoDao userFaceInfoMapper;

	@Autowired
    private CheckFaceService checkFaceService;
 

    @Override
    public void saveFace(BufferedImage img,String name,Long groupId,Integer gender,Integer age) throws Exception{
        String faceId = WorkId.sortUID(); 
        File outputfile = new File(HessionServiceConfig.staticFileSavePath+ File.separator + faceId + ".png");
        //ImageIO.write(img, "png", outputfile);
        
        ImageInfo imageInfo = ImageUtil.bufferedImage2ImageInfo(img);
        if(imageInfo == null) return ; 
        // 人脸特征获取
        List<ProcessInfo> features = faceEngineService.extractFaceFeature (imageInfo);
        if (CollectionUtil.isEmpty(features)) return;
        
        for(ProcessInfo feature : features){
           
            // 人脸特征插入到数据库 
        	UserFaceInfo userInfo = save(UserFaceInfo.builder().imgPath(outputfile.getAbsolutePath()).createTime(DateUtil.now()).checked(0).gender(gender).age(age).name(name).groupId(groupId).faceFeature (feature.getFaceFeature().getFeatureData()).faceId(faceId).build());
            // 人脸信息添加到缓存 
           // faceEngineService.addFaceToCache(groupId, FaceUserInfo.builder().id(userInfo.getId()).faceFeature (feature.getFaceFeature().getFeatureData()).name(name).faceId(faceId).build());
        	checkFaceService.addToCache(groupId, userInfo.getId());
        }
    }

    @Override
    public UserFaceInfo findByFaceId(String groupId){
        return userFaceInfoMapper.findByFaceId(groupId);
    }

	@Override
	public UserFaceInfo save(UserFaceInfo userFaceInfo) {
		userFaceInfoMapper.insert(userFaceInfo);
		return userFaceInfo;
	}
	
	@Override
    public void deleteInBatch(List<UserFaceInfo> users){ 
		userFaceInfoMapper.deleteBatchIds(users.stream().map(user -> user.getId()).collect(Collectors.toList()));
    }
	
	@Override
	public Page<UserFaceInfo> selectPage(Page<UserFaceInfo> page){
		Page<UserFaceInfo> users = super.selectPage(page);
		users.getRecords().stream().forEach(user -> {user.setGenderName((new Integer(1)).equals(user.getGender())?"男":"女");
		user.setCheckState((new Integer(1)).equals(user.getChecked())?"已检测到":"未检测到");});
		return users;
	} 
    @Override
	public Page<UserFaceInfo> selectPage(Page<UserFaceInfo> page, Wrapper<UserFaceInfo> wrapper) {
    	Page<UserFaceInfo> users = super.selectPage(page, wrapper);
    	users.getRecords().stream().forEach(user -> {user.setGenderName((new Integer(1)).equals(user.getGender())?"男":"女");
		user.setCheckState((new Integer(1)).equals(user.getChecked())?"已检测到":"未检测到");});
		return users;
	}
}
